/*
* Copyright 2011 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/
package org.etherframework.controller.common
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.utils.setTimeout;
	
	import mx.core.FlexGlobals;
	import mx.core.IFlexDisplayObject;
	import mx.core.Singleton;
	import mx.managers.IPopUpManager;
	
	import org.etherframework.common.command.ICommand;
	import org.etherframework.controller.IReactiveCommand;
	import org.etherframework.controller.ResponderCommand;
	
	import raix.reactive.IObservable;
	import raix.reactive.Observable;

	public class ShowPopUpCommand extends ResponderCommand
	{
		private var _type:Class;
		private var _model:PopUpModel;
		private var _parent:DisplayObject;
		private var _modal:Boolean;
		private var _manager:IPopUpManager;
		
		public function initialize(view:Class, model:PopUpModel, 
								   parent:DisplayObject = null, modal:Boolean = true):IReactiveCommand
		{
			_type = view;
			_parent = parent;
			_model = model;
			_modal = modal;
			_manager = IPopUpManager(Singleton.getInstance("mx.managers::IPopUpManager"));
			
			markAsInitialized();
			return this;
		}
		
		override public function observable():IObservable
		{
			var event:Event;
			const parent:DisplayObject = _parent || FlexGlobals.topLevelApplication as DisplayObject;
			const view:IModelPopUp = new _type() as IModelPopUp;
			
			if (!view)
				throw new Error("Component " + _type + " should implement IModelPopUp.");
			
			return Observable.defer(function():IObservable
			{
				view.model = _model;
				_model.popUp = view;
				
				if (ether.mediators.hasMediatorMapping(_type))
				{
					ether.mediators.createMediator(DisplayObject(view));
				}
				
				_manager.addPopUp(IFlexDisplayObject(view), parent, _modal);
				_manager.centerPopUp(IFlexDisplayObject(view));
				var completeEvent:IObservable = Observable.fromEvents(_model, 
					[Event.COMPLETE, Event.CANCEL]);
				var removeEvent:IObservable = Observable.fromEvents(IEventDispatcher(view), 
					[Event.REMOVED_FROM_STAGE]);
				
				return removeEvent.merge(completeEvent).take(1);
			})
			.mapMany(function(event:Event):IObservable
			{
				// dirty hack
				setTimeout(function():void {_manager.removePopUp(IFlexDisplayObject(view))}, 5);
				
				return Observable.value(event);
			});
		}
	}
}